/*
 * LabelDelegate.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "LabelDelegate.h"

#include "QIconAdapter.h"
#include "SwingDelegate.h"

// LabelDelegate :: Protected Constructor. -------------------------------------

/*!
 * \brief Constructs new LabelDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Label java object
 */
LabelDelegate::LabelDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QLabelAdapter())
{
  // empty constructor body
}

// LabelDelegate :: Native Calls. ----------------------------------------------

/*!
 * \brief Sets label text.
 *
 * \param env the JNIEnv pointer 
 * \param text the text 
 */
void LabelDelegate::setText(JNIEnv *env, jstring text)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setText, SWING_ENV_ARG, JSTRING_ARG(text))
  qLabelAdapter()->setText(QStringAdapter(env, text));
}

/*!
 * \brief Sets text format.
 *
 * \param env the JNIEnv pointer 
 * \param textFormat the text format
 */
void LabelDelegate::setTextFormat(JNIEnv *env, jint textFormat)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setTextFormat, SWING_ENV_ARG, JINT_ARG(textFormat))
  qLabelAdapter()->setTextFormat((Qt::TextFormat)textFormat);
}

/*!
 * \brief Sets text wrap status.
 *
 * \param env the JNIEnv pointer 
 * \param wordWrap if \c true the label will wrap the text
 */
void LabelDelegate::setWordWrap(JNIEnv *env, jboolean wordWrap)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setWordWrap, SWING_ENV_ARG, JBOOLEAN_ARG(wordWrap))
  qLabelAdapter()->setWordWrap(wordWrap);
}

/*!
 * \brief Sets text alignment.
 *
 * \param env the JNIEnv pointer 
 * \param ha the horizontal alignment
 * \param va the vertical alignment
 */
void LabelDelegate::setAlignment(JNIEnv *env, jint ha, jint va)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setAlignment, SWING_ENV_ARG, JINT_ARG(ha), JINT_ARG(va))
  qLabelAdapter()->setAlignment(SwingDelegate::toHorizontalAlignment(ha) |
                                SwingDelegate::toVerticalAlignment(va));
}

/*!
 * \brief Sets the icon set.
 *
 * \param env the JNIEnv pointer 
 * \param iconSet the icon set
 */
void LabelDelegate::setIcon(JNIEnv *env, jobject iconSet)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setIcon, SWING_ENV_ARG, JOBJECT_ARG(iconSet))
  if (iconSet) {
    qLabelAdapter()->setIcon(QIconAdapter(env, iconSet));
  } else {
    qLabelAdapter()->setIcon(QIcon());
  }
}

/*!
 * \brief Sets icon size.
 *
 * \param env the JNIEnv pointer 
 * \param width the icon width 
 * \param height the icon height 
 */
void LabelDelegate::setIconSize(JNIEnv *env, jint width, jint height)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setIconSize, SWING_ENV_ARG, JINT_ARG(width), JINT_ARG(height))
  qLabelAdapter()->setIconSize(QSize(width, height));
}

/*!
 * \brief Sets border type.
 *
 * \param env the JNIEnv pointer
 * \param borderType the border type
 */
void LabelDelegate::setBorderType(JNIEnv *env, jint borderType)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setBorderType, SWING_ENV_ARG, JINT_ARG(borderType))
  qLabelAdapter()->setFrameShape(QFrame::Shape(borderType));
}

/*!
 * \brief Sets border style.
 *
 * \param env the JNIEnv pointer
 * \param borderStyle the border style
 */
void LabelDelegate::setBorderStyle(JNIEnv *env, jint borderStyle)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setBorderStyle, SWING_ENV_ARG, JINT_ARG(borderStyle))
  qLabelAdapter()->setFrameShadow(QFrame::Shadow(borderStyle));
}

/*!
 * \brief Sets border width.
 *
 * \param env the JNIEnv pointer
 * \param borderWidth the border width
 */
void LabelDelegate::setBorderWidth(JNIEnv *env, jint borderWidth)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setBorderWidth, SWING_ENV_ARG, JINT_ARG(borderWidth))
  qLabelAdapter()->setMidLineWidth(borderWidth);
}

// LabelDelegate :: Static Native Calls. ---------------------------------------

/*!
 * \brief Initializes LabelDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the Label java class
 *
 * \exception FatalError if failed
 */
void LabelDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_UNUSED(env);
  Q_UNUSED(cls);
  // todo: Implement action links
}

/*!
 * \brief Returns new instance of the delegate object.
 *
 * \param env the JNIEnv pointer 
 * \param obj the Label java object
 * 
 * \return new instance of the delegate object
 */
jlong LabelDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new LabelDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
